const Game = require('./Game');
const Types = require('./module/Types');
const Utils = require('./module/Utils');
const ActorPlayingState = Types.ActorPlayingState;

// 将脚本挂载至渲染节点上
cc.Class({
    extends: cc.Component,

    properties: {
        playerInfo: cc.Node,
        // 桌上的筹码
        stakeOnTable: cc.Node,
        cardInfo: cc.Node,
        cardPrefab: cc.Prefab,
        anchorCards: cc.Node,
        spPlayerName: cc.Sprite,
        labelPlayerName: cc.Label,
        labelTotalStake: cc.Label,
        spPlayerPhoto: cc.Sprite,
        callCounter: cc.ProgressBar,
        labelStakeOnTable: cc.Label,
        spChips: {
            type: cc.Sprite,
            default: [],
        },
        // TODO 如何隐藏字段?
        labelCardInfo: cc.Label,
        spCardInfo: cc.Sprite,
        animFX: cc.Node,
        cardSpace: 0
    },

    onLoad: function() {
        cc.log("load actor renderer");
    },

    init: function (playerInfo, playerInfoPos, stakePos, turnDuration, switchSide) {
        cc.log('init actor renderer');
        this.actor = this.getComponent("Actor");
        // nodes
        this.isCounting = false;
        this.counterTimer = 0;
        this.turnDuration = turnDuration;

        this.playerInfo.position = playerInfoPos;
        this.stakeOnTable.position = stakePos;
        this.labelPlayerName.string = playerInfo.name;
        // 更新筹码
        this.updateTotalStake(playerInfo.gold);

        // 总共5位玩家
        const photoIdx = playerInfo.photoIdx % 5;
        this.spPlayerPhoto.spriteFrame = Game.instance.assetMng.playerPhotos[photoIdx];

        // fx
        this.animFX = this.animFX.getComponent("FXPlayer");
        this.animFX.init();
        this.animFX.show(false);
        this.cardInfo.active = false;

        this.cardInfo.active = false;
        if (switchSide) {
            this.spCardInfo.getComponent("SideSwitcher").switchSide();
            this.spPlayerName.getComponent("SideSwitcher").switchSide();
        }
    },

    update: function(dt) {
        if (this.isCounting) {
            this.callCounter.progress = this.counterTimer/this.turnDuration;
            this.counterTimer += dt;
            if (this.counterTimer >= this.turnDuration) {
                this.isCounting = false;
                this.callCounter.progress = 1;
            }
        }
    },

    updateTotalStake: function (num) {
        this.labelTotalStake.string = "$"+num;
    },

    // 初始化庄家
    initDealer: function () {
        this.actor = this.getComponent("Actor");
        this.animFX = this.animFX.getComponent("FXPlayer");
        this.animFX.init();
        this.animFX.show(false);
    },

    startCountdown: function() {
        if (this.callCounter) {
            this.isCounting = true;
            this.counterTimer = 0;
        }
    },

    playBlackJackFX: function() {
        this.animFX.playFX("blackjack");
    },

    playBustFX: function() {
        this.animFX.playFX("bust");
    },

    onDeal: function(card, show) {
        const newCard = cc.instantiate(this.cardPrefab).getComponent("Card");
        this.anchorCards.addChild(newCard.node);
        newCard.init(card);
        newCard.reveal(show);

        const startPos = cc.v2(0, 0);
        const index = this.actor.cards.length - 1;
        const endPos = cc.v2(this.cardSpace * index, 0);
        newCard.node.setPosition(startPos);
        this._updatePointPos(endPos.x);

        const moveAction = cc.moveTo(0.5, endPos);
        const callback = cc.callFunc(this._onDealEnd, this);
        newCard.node.runAction(cc.sequence(moveAction, callback));
    },

    _onDealEnd: function(target) {
        this.resetCountdown();
        if (this.actor.state === ActorPlayingState.Normal) {
            // 重新倒计时
            this.startCountdown();
        }
        this.updatePoint();
    },

    _updatePointPos: function(xPos) {
        cc.log(this.name + ' card info pos: ' + xPos);
        this.cardInfo.setPosition(xPos + 50, 0);
    },

    resetCountdown: function() {
        if (this.callCounter) {
            this.isCounting = false;
            this.counterTimer = 0;
            this.callCounter.progress = 0;
        }
    },

    onReset: function() {
        this.cardInfo.active = false;
        this.anchorCards.removeAllChildren();
        this._resetChips();
    },

    onRevealHoldCard: function() {
        const card = cc.find('cardPrefab', this.anchorCards).getComponent('Card');
        card.reveal(true);
        this.updateState();
    },

    updatePoint: function() {
        this.cardInfo.active = true;
        this.labelCardInfo.string = this.actor.bestPoint;

        switch (this.actor.hand) {
            case Types.Hand.BlackJack:
                // 21点
                this.animFX.show(true);
                this.animFX.playFX('blackjack');
                break;
            case Types.Hand.FiveCard:
                // TODO
                cc.log('five card');
                break;
        }
    },

    // 是否显示 1k, 2k,等筹码
    showStakeChips: function (stake) {
         const chips = this.spChips;
         let count = 0;
         if (stake > 50000) {
             count = 5;
         } else if (stake > 25000) {
             count = 4;
         } else if (stake > 10000) {
             count = 3;
         } else if (stake > 5000) {
             count = 2;
         } else if (stake > 0) {
             count = 1;
         }
         for (let i = 0; i < count; ++i) {
             chips[i].enabled = true;
         }
    },

    _resetChips: function () {
        for (let i = 0; i < this.spChips.length; ++i) {
            this.spChips.enabled = false;
        }
    },

    updateState: function () {
        cc.log('update state');
        switch (this.actor.state) {
            case ActorPlayingState.Normal:
                this.cardInfo.active = true;
                this.spCardInfo.spriteFrame = Game.instance.assetMng.texCardInfo;
                this.updatePoint();
                break;
            case ActorPlayingState.Bust:
                const min = Utils.getMinMaxPoint(this.actor.cards).min;
                this.labelCardInfo.string = `爆牌(${min})`;
                this.spCardInfo.spriteFrame = Game.instance.assetMng.texBust;
                this.cardInfo.active = true;
                this.animFX.show(true);
                this.animFX.playFX('bust');
                break;
            case ActorPlayingState.Stand:
                const max = Utils.getMinMaxPoint(this.actor.cards).max;
                this.labelCardInfo.string = `停牌(${max})`;
                this.spCardInfo.spriteFrame = Game.instance.assetMng.texCardInfo;
                this.resetCountdown();
                break;
        }
    }
});
